package ee.maoinvest.frontend.controller.eventfinders;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class RegularReadingsMenuEventFinder implements EventFinder
{
	private final String EMPTY_STRING = "";
	private Logger logger = Logger.getLogger(RegularReadingsMenuEventFinder.class);
	
	@Override
	public String findEvent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		String event = "undefined";
		
		try 
		{
			String mode = request.getParameter("mode");
			String submode = request.getParameter("submode");
			String formSubmitted = request.getParameter("formSubmitted");
			
			if (mode.equals("administerRegularReadings") && submode != null && !submode.equals(EMPTY_STRING))
			{
				event = "undefined";
				
				if (submode.equals("startNewRegularReadingPage"))
				{
					event = "startNewRegularReadingPage";
				}
	
				if (submode.equals("startNewRegularReadingPage")
						&& formSubmitted != null
						&& formSubmitted.equals("startNewRegularReadingForm"))
				{
					event = "startNewRegularReadingFormPostEvent";
				}
				
				if (submode.equals("saveElectricityMeterRegularReadingPage"))
				{
					event = "saveElectricityMeterRegularReadingPage";
				}
	
				if (submode.equals("saveElectricityMeterRegularReadingPage")
						&& formSubmitted != null
						&& formSubmitted.equals("saveElectricityMeterRegularReadingForm"))
				{
					event = "saveElectricityMeterRegularReadingFormPostEvent";
				}
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return event;
	}
}